package com.starmark.sys.login.provider.service;

import com.starmark.core.framework.util.BeanMapper;
import com.starmark.sys.login.api.dto.UserInfoDto;
import com.starmark.sys.login.api.dto.UserLoginToken;
import com.starmark.sys.login.api.service.ISysLoginService;
import com.starmark.sys.org.api.dto.OrgUserInfoDto;
import com.starmark.sys.org.api.service.IOrgUserApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统默认登陆
 */
@Service
public class SysLoginDefaultService implements ISysLoginService {

    private static final String LOGIN_DEFAULT_LOGIN_TYPE = "default";

    /**
     * 组织用户服务API
     */
    @Autowired
    private IOrgUserApiService orgUserApiService;


    @Override
    public UserInfoDto loadUserByToken(UserLoginToken userLoginToken) {
        Map<String,Object> userMap=new HashMap<>();
        userMap.put("login_name",userLoginToken.getUsername());
        OrgUserInfoDto orgUserInfoDto=orgUserApiService.loadUserInfoByMap(userMap);
        return BeanMapper.map(orgUserInfoDto,UserInfoDto.class);
    }

    @Override
    public boolean isSupportLogin(String loginType) {
        return LOGIN_DEFAULT_LOGIN_TYPE.equalsIgnoreCase(loginType);
    }

    @Override
    public boolean isPasswordMatch(UserLoginToken userLoginToken,  UserInfoDto userInfoDto) {

        return orgUserApiService.checkUserPassword(userInfoDto.getId(),userLoginToken.getPassword());
    }
}
